#!/bin/bash
#默认变量@@group@@ 定义包含多组“切换指令集合”，可用于代码循环编程
#group= @@group@@
#group变量中包含多组指令集
#@@linkstring@@定义为连接信息数据数组，可用远程访问设备，数据信息包括“IP、访问协议、端口、用户名、密码”五个字段，数据来源于【设备密码管理】功能

######################################################################
# 功能说明：创建并检查mysql主从
# 参数说明：
# check_times: 尝试检查次数
# IP: GPFS服务器IP
# user：GPFS服务器登陆用户名
# password：GPFS服务器登陆密码
#######################################################################

# typeset MIP='@@MIP@@'
# typeset SIP='@@SIP@@'
# typeset huser='@@USER@@'
# typeset hpassword='@@PASSWORD@@'
# typeset dbmuser='@@DBUSER@@'
# typeset dbmpassword='@@DBPASSWORD@@'
# typeset dbsuser='@@DBUSER@@'
# typeset dbspassword='@@DBPASSWORD@@'
# typeset check_times='@@check_times@@'

typeset MIP='10.27.3.175'
typeset SIP='10.27.3.176'
typeset huser='root'
typeset hpassword='1qaz@WSX'
typeset dbmuser='root'
typeset dbmpassword='QWEasdzxc#123'
typeset dbsuser='root'
typeset dbspassword='QWEasdzxc#123'
typeset check_times=''

#初始化选填参数
[ ! ${check_times} ] && typeset check_times='3'

#参数检查
expr 1 + ${check_times} &>/dev/null
if [ $? -ne 0 ];then
    echo 'Argument needs to be integer';exit 1
fi

#定义日志目录
typeset SCRIPT_NAME="mysql_ms_$MIP"
typeset LOG_PATH='/tmp/dr_log'
[ ! -d ${LOG_PATH} ] && mkdir -p ${LOG_PATH}
typeset LOG_FILE="${LOG_PATH}/${SCRIPT_NAME}.log"
[ ! -f ${LOG_FILE} ] && touch ${LOG_FILE}

function log_format()
{
    echo "[`date +"%F %H:%M:%S"`] [$*]" | tee -a ${LOG_FILE}
}

function main()
{
    #检查文件系统是否卸载成功
    for ((i=0;i<${check_times};i++))
    do
        sleep 10 && log_format "-------------------- ${i} inspections --------------------"
        log_format "cmd:mmlsmount all | grep 'not mounted'"
        /opt/hatech/code/scripts/GPFS/remote_control.exp $IP $user $password "mmlsmount all " | grep "not mounted" | grep -v grep | tee -a ${LOG_FILE}
        local mount_num=$(/opt/hatech/code/scripts/GPFS/remote_control.exp $IP $user $password "mmlsmount all" | grep "not mounted" | grep -v grep | wc -l) 
        if [ ${mount_num} -eq 0 ]
		then
            continue
        else
            log_format "INFO:" "Umount GPFS success."
            return 0
        fi
    done
    log_format "ERROR:" "Umount GPFS failed."
    
    return 1
}

main $@
if [ $? -eq 0 ];then
    log_format "script_execute_success!"
else
    log_format "script_execute_error!"
    exit 1
fi